package com.car.loan.project.cm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.enums.SwitchStatus;
import com.car.loan.common.enums.UserStatus;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.project.cm.domain.CmDealer;
import com.car.loan.project.cm.mapper.CmDealerMapper;
import com.car.loan.project.cm.service.ICmDealerService;
import com.car.loan.project.cm.util.vo.SelectedVo;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 业务经销商Service业务层处理
 *
 * @author ruoyi
 * @date 2019-10-19
 */
@Service
public class CmDealerServiceImpl implements ICmDealerService {

    @Autowired
    private CmDealerMapper cmDealerMapper;

    /**
     * 查询业务经销商
     *
     * @param id 业务经销商ID
     * @return 业务经销商
     */
    @Override
    public CmDealer selectCmDealerById(Long id) {
        return cmDealerMapper.selectById(id);
    }

    @Override
    public boolean checkDealerNameUnique(String name, Long id) {
        CmDealer queryDealer = new CmDealer();
        queryDealer.setName(name);
        QueryWrapper<CmDealer> queryWrapper = new QueryWrapper<>(queryDealer);
        CmDealer cmDealer = cmDealerMapper.selectOne(queryWrapper);
        return cmDealer == null || id.equals(cmDealer.getId());
    }

    /**
     * 查询业务经销商列表
     *
     * @param cmDealer 业务经销商
     * @return 业务经销商
     */
    @Override
    public List<CmDealer> selectCmDealerList(CmDealer cmDealer) {
        QueryWrapper<CmDealer> queryWrapper = new QueryWrapper<>(cmDealer);
        return cmDealerMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<CmDealer> selectPage(IPage<CmDealer> page,
        QueryWrapper<CmDealer> queryWrapper) {
        return cmDealerMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<SelectedVo> selectedPage(IPage<CmDealer> page, String name, Integer of) {
        QueryWrapper<CmDealer> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like("name", name);
        }
        queryWrapper.select("id,name,status");
        if (null == of || SwitchStatus.OPEN.status == of) {
            queryWrapper.eq("status", UserStatus.OK.getCode());
        } else {
            queryWrapper.in("status", UserStatus.OK.getCode(), UserStatus.DISABLE.getCode());
        }
        queryWrapper.orderByDesc("id");
        IPage<CmDealer> dealerIPage = cmDealerMapper.selectPage(page, queryWrapper);
        List<CmDealer> records = dealerIPage.getRecords();
        IPage<SelectedVo> result = new Page<>();
        BeanUtils.copyBeanProp(result, dealerIPage);
        List<SelectedVo> selectedVos = new ArrayList<>();
        if (StringUtils.isNotEmpty(records)) {
            records.forEach(cmDealer -> {
                selectedVos.add(new SelectedVo().setValue(String.valueOf(cmDealer.getId()))
                    .setLabel(cmDealer.getName()).setStatus(cmDealer.getStatus()));
            });
        }
        result.setRecords(selectedVos);
        return result;
    }

    /**
     * 新增业务经销商
     *
     * @param cmDealer 业务经销商
     * @return 结果
     */
    @Override
    public int insertCmDealer(CmDealer cmDealer) {
        cmDealer.setCreateTime(LocalDateTime.now());
        return cmDealerMapper.insert(cmDealer);
    }

    /**
     * 修改业务经销商
     *
     * @param cmDealer 业务经销商
     * @return 结果
     */
    @Override
    public int updateCmDealer(CmDealer cmDealer) {
        return cmDealerMapper.updateById(cmDealer);
    }

    /**
     * 删除业务经销商对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCmDealerByIds(Collection<Long> ids) {
        return cmDealerMapper.deleteBatchIds(ids);
    }

    /**
     * 删除业务经销商信息
     *
     * @param id 业务经销商ID
     * @return 结果
     */
    @Override
    public int deleteCmDealerById(Long id) {
        return cmDealerMapper.deleteById(id);
    }
}
